package base.variable;

public class StringDemo {
    public static void main(String[] args) {
        String str = "Hello", str2 = new String("World");
        System.out.println("str = " + str + ", str2 = " + str2);
        System.out.println("str == str2: " + str.equals(str2));
        String str2Copy = new String(str2);
        System.out.println("str2Copy = " + str2Copy);
        System.out
                .println("str2 == str2Copy: " + str2.equals(str2Copy) + ", &str2Copy == &str2: " + (str2 == str2Copy));
        String prefix = str + " ";
        String phrase = prefix.concat(str2);
        // extract string part
        System.out.println(String.format("length of '%s' = %d", phrase, phrase.length()));
        System.out.println(String.format("phrase = '%s'", phrase));
        System.out.println(String.format("phrase[%d] = %s", 0, phrase.charAt(0)));
        String part = "l";
        System.out.println(String.format("left '%s' of phrase at %d", part, phrase.indexOf(part)));
        System.out.println(String.format("right '%s' of phrase at %d", part, phrase.lastIndexOf(part)));
        System.out.println(String.format("phrase[%d:%d] = %s", 0, 5, phrase.substring(0, 5)));
        String raw = "   title  ";
        System.out.println(String.format("raw = '%s', trimmed = '%s'", raw, raw.trim()));
        // changable String object
        StringBuilder sb1 = new StringBuilder();
        sb1.append(str2Copy);
        System.out.println(String.format("sb1 = '%s', capacity = '%d'", sb1.toString(), sb1.capacity()));
        StringBuilder sb2 = new StringBuilder(50);
        System.out.println(String.format("sb2 = '%s', capacity = '%d'", sb2.toString(), sb2.capacity()));
        StringBuilder sb3 = new StringBuilder("Hello");
        System.out.println(String.format("sb3 = '%s', capacity = '%d'", sb3.toString(), sb3.capacity()));
        int i = 3;
        char c = sb3.charAt(i);
        sb3.deleteCharAt(i);
        System.out.println(String.format("sb3[%d] = '%s', sb3 = '%s' after delete it", i, c, sb3.toString()));
    }
}
